#pragma once

#include <string>
#include "../Common/QuitableThread.hpp"
#include "../Common/SafeQueue.hpp"
#include "../Common/Packet.hpp"
#include "../Common/SocketIOManager.hpp"

namespace cybertron {
	class LogPacketSender : public SocketIO {
	public:
		LogPacketSender(SOCKET s, const union_endpoint &ep, SafeQueue<NetPacket> *send_queue) : SocketIO(s, ep, _SOCKET_IO_READ | _SOCKET_IO_WRITE)
			, _connected(false)
			, _ongoing_packet(nullptr)
			, _send_queue(send_queue) {};

		virtual ~LogPacketSender() {
			if (_ongoing_packet) {
				delete _ongoing_packet;
			}
		};

		virtual int on_read();
		virtual int on_write();

		bool connected() { return _connected; };
	private:
		bool _connected;
		NetPacket *_ongoing_packet;
		SafeQueue<NetPacket> *_send_queue;
	};


	class LogSender : public QuitableThread {
	public:
		LogSender(const std::string& server, uint16_t port, SafeQueue<NetPacket> *send_queue);
		virtual ~LogSender() {};

	private:
		virtual void run_once();
		std::string _server;
		uint16_t _port;
		SafeQueue<NetPacket> *_send_queue;
		SocketIOManager _io_manager;
	};
}